﻿using System;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class TestCommand : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Result result;
			try
			{
				SearchSystem.LogEnabled = true;
				Log.Enabled = true;
				UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
				string title = activeUIDocument.Document.Title;
				SavePipe.FileName = ((!title.Contains(".rvt")) ? title : title.Substring(0, title.LastIndexOf(".rvt")));
				MainForm mainForm = new MainForm(activeUIDocument);
				DialogResult dialogResult = mainForm.ShowDialog();
				while (dialogResult == DialogResult.Retry || dialogResult == DialogResult.Abort)
				{
					if (dialogResult != DialogResult.Abort)
					{
						mainForm = new MainForm(activeUIDocument);
					}
					dialogResult = mainForm.ShowDialog();
				}
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Cancelled;
			}
			catch (Exception ex)
			{
				YJKMessageBox.Information(ex.Message);
				result = Autodesk.Revit.UI.Result.Failed;
			}
			return result;
		}
	}
}
